package com.cnunicom.modules.tra.controller;

import com.cnunicom.common.utils.DateUtils;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.R;
import com.cnunicom.modules.tra.entity.ActivityEntity;
import com.cnunicom.modules.tra.entity.UnitEntity;
import com.cnunicom.modules.tra.service.ActivityService;
import com.cnunicom.modules.tra.service.UnitService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * 活动管理表
 *
 * @author dzygod
 * @email dingziyuan0@gmail.com
 * @date 2019-11-27 21:39:33
 */
@RestController
@RequestMapping("tra/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @Autowired
    private UnitService unitService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("tra:activity:list")
    public R list(@RequestParam Map<String, Object> params) {
        List<UnitEntity> list = unitService.list();
        PageUtils page = activityService.queryPage(params);
        List<ActivityEntity> entities = activityService
                .queryUnitsList((List<ActivityEntity>) page.getList(), list);
        page.setList(entities);
        return Objects.requireNonNull(R.ok().put("page", page)).put("units", list.toArray());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("tra:activity:info")
    public R info(@PathVariable("id") Integer id) {
        ActivityEntity activity = activityService.getById(id);
        List<UnitEntity> units = unitService.query().select("id")
                .in("id", activity.getUnitId()).list();
        Object[] unitids = units.stream().map(UnitEntity::getId).toArray();
        return Objects.requireNonNull(R.ok().put("activity", activity)).put("unitids", unitids);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("tra:activity:save")
    public R save(@RequestBody @Valid ActivityEntity activity) {

        Integer isHasId = activityService.query()
                .like(StringUtils.isNotBlank(activity.getUnitId()), "unit_id", activity.getUnitId())
                .count();
        Integer isNull = activityService.query().eq("unit_id", "").count();

        if (isHasId > 0 & isNull > 0) {
            return R.error("所选单位已被关联！请重新选择！");
        }
        if (isNull > 0) {
            return R.error("全局活动已经存在，无法新增！");
        }

        /*
            注册时间
         */
        activity.setCreateTime(DateUtils.getDateTime());
        activityService.save(activity);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("tra:activity:update")
    public R update(@RequestBody ActivityEntity activity) {
        activityService.updateById(activity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("tra:activity:delete")
    public R delete(@RequestBody Integer[] ids) {
        activityService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 单位二维码，渲染时跳转
     *
     * @param unitId
     * @return
     */
    @RequestMapping("/wapHome/unit/{unitId}")
    public R unitSuccess(@PathVariable("unitId") String unitId) {

        /*
            根据单位id进行查询，查询活动表里是否有这个活动，如果没有这个活动，那么就给他返回全局活动
         */
        List<ActivityEntity> entities = activityService.query().like("unit_id", unitId)
                .eq("status", 0).list();

        // 活动表里没有活动与这个单位关联，那么就把全局活动给手机端
        if (entities.size() == 0) {
            entities = activityService.query().eq("unit_id", "").list();
        }

        return R.ok().put("activity", entities);
    }

    /**
     * 获取全局活动
     * @return 返回活动内容
     */
    @RequestMapping("/wapHome/globalActivity")
    public R globalActivity() {

        // 把全局活动给手机端
        List<ActivityEntity> entities = activityService.query().eq("unit_id", "").list();

        return R.ok().put("activity", entities);
    }
}
